package cn.dujc.superkid.fragment.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.adapter.entity.IExpandable;
import cn.dujc.core.ui.BaseListFragment;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.activities.ExaminationActivity;
import cn.dujc.superkid.activities.LoginActivity;
import cn.dujc.superkid.adapter.ExamineListAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.ChannelEntity;
import cn.dujc.superkid.bean.ExamineListResult;
import cn.dujc.superkid.bean.Main2Entity;
import cn.dujc.superkid.toolbar.ToolbarKeeper;
import cn.dujc.superkid.utils.Preferences;


/**
 * A simple {@link Fragment} subclass.
 */
public class TestFragment extends BaseListFragment {
    private final List<IExpandable> mList = new ArrayList<>();
    private ExamineListAdapter examineListAdapter;
    private String channel_id="";
    private String channelid;


    @Nullable
    @Override
    public Toolbar initToolbar(ViewGroup parent) {
        return ToolbarKeeper.initFragmentToolbar(parent, "考试");
    }

    protected void recyclerViewOtherSetup() {
        if (mList != null) {
            //去掉下划线
//            mRvList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
//            myQuestionAdapter.disableLoadMoreIfNotFullPage();//禁止加载
            getSwipeRefreshLayout().setEnabled(false);//禁止下拉刷新
        }
    }

    @Nullable
    @Override
    protected BaseQuickAdapter initAdapter() {
        examineListAdapter = new ExamineListAdapter(mList);
        return examineListAdapter;
    }

    @Override
    protected void onItemClick(int position) {

    }

    @Override
    protected void loadMore() {
        refreshDone();
        loadDone(true, true);
        loadData();
    }

    @Override
    protected void reload() {
        loadData();
    }

    private void loadData() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }
        channel_id= Preferences.getInstance().getChannel(mActivity).getChannel_id();

        HttpHelper.getExamineOnline().examineList(QMap.build()
                .put("channel",channel_id)
                .create()).enqueue(new ResultCallBack<Result<List<ExamineListResult>>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<List<ExamineListResult>> body) {
                mList.clear();
                if (body != null) {
                    final List<ExamineListResult> data = body.getData();
                    for (int i = 0; i < data.size(); i++){
                        String title = data.get(i).getGname();
                        Main2Entity.InnerEntity[] child = new Main2Entity.InnerEntity[data.get(i).getExamine_list().size()];
                        for (int inner = 0; inner < child.length; inner++) {
                            child[inner] = new Main2Entity.InnerEntity(data.get(i).getExamine_list().get(inner));
                        }
                        mList.add(new Main2Entity(title, child));
                    }
                }
                notifyDataSetChanged(true);
            }

            @Override
            public void onFailureCallback(int code, String msg) {
                examineListAdapter.setEmptyView(R.layout.layout_empty_img);
            }
        });
    }

    public void onResume() {
        super.onResume();
        getChannel();
    }
    private void getChannel() {
        channel_id=Preferences.getInstance().getChannel(mActivity).getChannel_id();
        if (channel_id.equals(channelid)){
        }else{
            channelid=channel_id;
            mList.clear();
            notifyDataSetChanged(true);
            loadMore();
        }

    }

}
